import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import qiankun from 'vite-plugin-qiankun'

// 直接在文件中定义isDevelopment函数，避免模块导入问题
function isDevelopment() {
    return process.env.NODE_ENV !== 'production';
}

export default defineConfig({
    plugins: [
        vue(),
        // 使用qiankun插件
        qiankun('microfrontend-host', {
            useDevMode: true
        })
    ],
    server: {
        port: 5001,
        proxy: isDevelopment() ? {
            // 微应用代理
            '/legacy': {
                target: 'https://vue.ruoyi.vip',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/legacy/, '')
            },
            '/static': {
                target: 'https://vue.ruoyi.vip',
                changeOrigin: true,
            },
            '/prod-api': {
                target: 'https://vue.ruoyi.vip',
                changeOrigin: true,
            },
            // API代理
            '/api': {
                target: 'http://localhost:8080',
                changeOrigin: true
            }
        } : {},
        cors: true
    },
    resolve: {
        alias: {
            '@': resolve(__dirname, 'src'),
            '@shared': resolve(__dirname, '../../packages/shared/src'),
            '@configs': resolve(__dirname, '../../packages/configs/src')
        }
    }
})